//
//  ReportCardDetailVC.m
//  jzyParents
//
//  Created by 任辰东 on 2022/9/19.
//

#import "ReportCardDetailVC.h"
#import "ReportCardTitleView.h"
#import "ReportApis.h"
#import "ReportCardTiDetailModel.h"
#import "DaTiYongShiView.h"
#import "JunFenQingKuangView.h"
#import "RCDWebView.h"
#import "UIButton+BackgroundColor.h"

@interface ReportCardDetailVC ()
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIButton *allBtn;
@property (nonatomic, strong) UIButton *cuoBtn;

@property (nonatomic, strong) UIView *tiganView;
@property (nonatomic, strong) ReportCardTitleView *titleView11;
@property (nonatomic, strong) UIView *xuekeBgView;
@property (nonatomic, strong) UILabel *xuekeLabel;
@property (nonatomic, strong) UILabel *markLabel;
@property (nonatomic, strong) UIImageView *contentImageView;

@property (nonatomic, strong) UIView *daanView;
@property (nonatomic, strong) ReportCardTitleView *daanTitleView;
@property (nonatomic, strong) UILabel *daanLabel;
@property (nonatomic, strong) UIImageView *daanImageView;

@property (nonatomic, strong) UIView *jeixiView;
@property (nonatomic, strong) ReportCardTitleView *jiexiTitleView;
@property (nonatomic, strong) RCDWebView *myWebView;

@property (nonatomic, strong) UIView *zhishidianView;
@property (nonatomic, strong) ReportCardTitleView *zhishidianTitleView;
@property (nonatomic, strong) UILabel *zhishidianLabel;

@property (nonatomic, strong) DaTiYongShiView *yongshiView;
@property (nonatomic, strong) JunFenQingKuangView *junfenView;

@property (nonatomic, strong) ReportCardModel *model;
@property (nonatomic, strong) NSArray <Xiaoti_score_tongjiItem *>*menuArray;

@property (nonatomic, strong) Xiaoti_score_tongjiItem *tiModel;
@end

@implementation ReportCardDetailVC

-(instancetype)initWithReportCardModel:(ReportCardModel *)model withXiaoti_score_tongjiItem:(Xiaoti_score_tongjiItem *)ti_model
{
    self = [super init];
    if (self) {
        _model = model;
        _tiModel = ti_model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"试题详情";
    [self getData];
}

- (void)getData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"id"] = [GVUserDefaults standardUserDefaults].student_id;
    params[@"main_id"] = self.model.main_id;
    params[@"kaoshi_type"] = self.model.is_liankao;
    params[@"ti_id"] = self.tiModel.ti_id;
    WeakSelf
    [self showLoadingHUDWithMessage:nil];
    [ReportApis ctb_Xq_ShitiDetailRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        ReportCardTiDetailModel *model = [ReportCardTiDetailModel mj_objectWithKeyValues:responseData];
        [self configView:model];
        
    } FailureBlock:^(NSNumber *code, NSString *msg) {
        StrongSelf
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];
}

-(void)configView:(ReportCardTiDetailModel *)model
{
    switch (model.ti_type.integerValue) {
        case 1:
        {
            self.xuekeLabel.text = @"单选";
        }
            break;
        case 2:
        {
            self.xuekeLabel.text = @"多选";
        }
            break;
        case 3:
        {
            self.xuekeLabel.text = @"填空";
        }
            break;
        case 4:
        {
            self.xuekeLabel.text = @"简答";
        }
            break;
            
        default:
            break;
    }
    self.markLabel.text = [NSString stringWithFormat:@"第%@题  分值%@",self.tiModel.ti_name_show,model.ti_score];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.ti_title]];
    UIImage *image = [UIImage imageWithData:data];
    CGSize imageSize = image.size;
    [self.contentImageView mas_updateConstraints:^(MASConstraintMaker *make) {
       
        make.height.mas_equalTo(imageSize.height/imageSize.width*(kScreenWidth - 30));
    }];
    [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:model.ti_title]];
    
    if (model.ti_type.integerValue == 1 || model.ti_type.integerValue == 2) {
        self.daanImageView.hidden = YES;
        self.daanLabel.hidden = NO;
        self.daanLabel.text = model.daan_true;
        
        [self.daanLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.top.equalTo(self.daanTitleView.mas_bottom).offset(10);
            make.left.equalTo(self.daanView).offset(15);
            make.right.equalTo(self.daanView).offset(-15);
            make.bottom.equalTo(self.daanView).offset(-15);
        }];
        
        [self.daanImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.top.equalTo(self.daanTitleView.mas_bottom).offset(10);
            make.height.mas_equalTo(0);
            make.left.equalTo(self.daanView).offset(15);
            make.right.equalTo(self.daanView).offset(-15);
        }];
    }else{
        
        if (IsEmpty(model.my_datika)) {
            return;
        }
        
        self.daanImageView.hidden = NO;
        self.daanLabel.hidden = YES;
        NSData *datas = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.my_datika]];
        UIImage *images = [UIImage imageWithData:datas];
        CGSize imagesSize = images.size;
        [self.daanImageView sd_setImageWithURL:[NSURL URLWithString:model.my_datika]];
        
        [self.daanLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.top.equalTo(self.daanTitleView.mas_bottom).offset(10);
            make.left.equalTo(self.daanView).offset(15);
            make.right.equalTo(self.daanView).offset(-15);
            
        }];
        
        [self.daanImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.top.equalTo(self.daanTitleView.mas_bottom).offset(10);
            make.height.mas_equalTo(imagesSize.height/imagesSize.width*(kScreenWidth - 30));
            make.left.equalTo(self.daanView).offset(15);
            make.right.equalTo(self.daanView).offset(-15);
            make.bottom.equalTo(self.daanView).offset(-15);
        }];
    }
    
    self.zhishidianLabel.text = model.guanlian_zhishidian;
    [self.myWebView loadRequestWithRelativeUrl:model.jiexi];
    [self.yongshiView configViewWithModel:model];
    [self.junfenView configViewWithModel:model];
    
}

-(void)setupViews
{
    [self.bgScrollView.contentView addSubview:self.tiganView];
    [self.tiganView addSubview:self.titleView11];
    [self.tiganView addSubview:self.xuekeBgView];
    [self.tiganView addSubview:self.xuekeLabel];
    [self.tiganView addSubview:self.markLabel];
    [self.tiganView addSubview:self.contentImageView];
    
    [self.bgScrollView.contentView addSubview:self.daanView];
    [self.daanView addSubview:self.daanTitleView];
    [self.daanView addSubview:self.daanLabel];
    [self.daanView addSubview:self.daanImageView];
    
    [self.bgScrollView.contentView addSubview:self.jeixiView];
    [self.jeixiView addSubview:self.jiexiTitleView];
    [self.jeixiView addSubview:self.myWebView];
    
    [self.bgScrollView.contentView addSubview:self.zhishidianView];
    [self.zhishidianView addSubview:self.zhishidianTitleView];
    [self.zhishidianView addSubview:self.zhishidianLabel];
    
    [self.bgScrollView.contentView addSubview:self.yongshiView];
    [self.bgScrollView.contentView addSubview:self.junfenView];
    

    [self.myWebView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
    self.myWebView.scrollView.scrollEnabled = NO;
    
    [self.bgScrollView.contentView layoutIfNeeded];
    [self.myWebView layoutSubviews];
    
}

-(void)setupLayout
{

    [self.tiganView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.right.equalTo(self.bgScrollView.contentView);
    }];
    
    [self.titleView11 mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.right.equalTo(self.tiganView);
    }];
    
    [self.xuekeBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.titleView11.mas_bottom).offset(15);
        make.left.equalTo(self.tiganView).offset(15);
        make.height.mas_equalTo(15);
    }];
    
    [self.xuekeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        CGFloat width = [@"简答" widthWithFont:UIFontSize(11) constrainedToHeight:15];
        make.left.equalTo(self.xuekeBgView).offset(8);
        make.right.equalTo(self.xuekeBgView).offset(-8);
        make.top.bottom.equalTo(self.xuekeBgView);
        make.width.mas_offset(width);
    }];
    
    [self.markLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.xuekeBgView).offset(0);
        make.left.equalTo(self.xuekeBgView.mas_right).offset(6);
        make.right.equalTo(self.tiganView).offset(-15);
    }];
    
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.xuekeBgView.mas_bottom).offset(10);
        make.left.equalTo(self.tiganView).offset(15);
        make.right.equalTo(self.tiganView).offset(-15);
        make.bottom.equalTo(self.tiganView.mas_bottom).offset(-15);
    }];
    
    [self.daanView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.tiganView.mas_bottom);
        make.left.right.equalTo(self.bgScrollView.contentView);
    }];
    
    [self.daanTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.daanView);
    }];
    
    [self.daanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.daanTitleView.mas_bottom).offset(10);
        make.left.equalTo(self.daanView).offset(15);
        make.right.equalTo(self.daanView).offset(-15);
        make.bottom.equalTo(self.daanView).offset(-15);
    }];
    
    [self.daanImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.daanTitleView.mas_bottom).offset(10);
        make.left.equalTo(self.daanView).offset(15);
        make.right.equalTo(self.daanView).offset(-15);
        make.bottom.equalTo(self.daanView).offset(-15);
    }];
    
    [self.jeixiView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.daanView.mas_bottom);
        make.left.right.equalTo(self.bgScrollView.contentView);
    }];
    
    [self.jiexiTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.jeixiView);
    }];
    
    [self.myWebView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.jiexiTitleView.mas_bottom).offset(10);
        make.left.equalTo(self.jeixiView).offset(15);
        make.right.equalTo(self.jeixiView).offset(-15);
        make.bottom.equalTo(self.jeixiView).offset(-15);
    }];
    
    [self.zhishidianView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.jeixiView.mas_bottom);
        make.left.right.equalTo(self.bgScrollView.contentView);
    }];
    
    [self.zhishidianTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.zhishidianView);
    }];
    
    [self.zhishidianLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.zhishidianTitleView.mas_bottom).offset(10);
        make.left.equalTo(self.zhishidianView).offset(15);
        make.right.equalTo(self.zhishidianView).offset(-15);
        make.bottom.equalTo(self.zhishidianView).offset(-15);
    }];
    
    [self.yongshiView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.zhishidianView.mas_bottom);
        make.left.right.equalTo(self.bgScrollView.contentView);
    }];
    
    [self.junfenView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.yongshiView.mas_bottom);
        make.left.right.equalTo(self.bgScrollView.contentView);
        make.bottom.equalTo(self.bgScrollView.contentView);
    }];
}

#pragma mark -- KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{

    if (object != self.myWebView.scrollView) {
        return;
    }
    
    if ([keyPath isEqualToString:@"contentSize"]) {
        
        CGSize size=[change[NSKeyValueChangeNewKey] CGSizeValue];
        if (size.height > 0) {
            [self.myWebView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(size.height);
            }];
        }
        DLog(@"size.height === %f",size.height);
    }
}


#pragma mark -- lazyLoad

-(UIView *)tiganView
{
    if (!_tiganView) {
        _tiganView = [UIView new];
        _tiganView.backgroundColor = UIColorFromHex(kColorWhite);
    }
    return _tiganView;
}

-(ReportCardTitleView *)titleView11
{
    if (!_titleView11) {
        _titleView11 = [ReportCardTitleView new];
        [_titleView11 configViewWithTitle:@"题干"];
    }
    return _titleView11;
}

-(UIView *)xuekeBgView
{
    if (!_xuekeBgView) {
        _xuekeBgView = [UIView new];
        _xuekeBgView.backgroundColor = UIColorFromHex(kColorMain);
        _xuekeBgView.layer.cornerRadius = 7.5;
        _xuekeBgView.layer.masksToBounds = YES;
    }
    return _xuekeBgView;
}

-(UILabel *)xuekeLabel
{
    if (!_xuekeLabel) {
        _xuekeLabel = [UILabel new];
        _xuekeLabel.textColor = UIColorFromHex(kColorWhite);
        _xuekeLabel.font = UIFontSize(11);
        _xuekeLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _xuekeLabel;
}

-(UILabel *)markLabel
{
    if (!_markLabel) {
        _markLabel = [UILabel new];
        _markLabel.textColor = UIColorFromHex(0x000000);
        _markLabel.font = UIFontSize(11);
    }
    return _markLabel;
}

-(UIImageView *)contentImageView
{
    if (!_contentImageView) {
        _contentImageView = [UIImageView new];
        _contentImageView.contentMode = UIViewContentModeScaleAspectFit;
        _contentImageView.clipsToBounds = YES;
    }
    return _contentImageView;
}

-(UIView *)daanView
{
    if (!_daanView) {
        _daanView = [UIView new];
        _daanView.backgroundColor = UIColorFromHex(kColorWhite);
    }
    return _daanView;
}

-(ReportCardTitleView *)daanTitleView
{
    if (!_daanTitleView) {
        _daanTitleView = [ReportCardTitleView new];
        [_daanTitleView configViewWithTitle:@"答题情况"];
    }
    return _daanTitleView;
}

-(UILabel *)daanLabel
{
    if (!_daanLabel) {
        _daanLabel = [UILabel new];
        _daanLabel.textColor = UIColorFromHex(0x000000);
        _daanLabel.font = UIFontSize(15);
    }
    return _daanLabel;
}

-(UIImageView *)daanImageView
{
    if (!_daanImageView) {
        _daanImageView = [UIImageView new];
        _daanImageView.contentMode = UIViewContentModeScaleAspectFit;
        _daanImageView.clipsToBounds = YES;
    }
    return _daanImageView;
}

-(UIView *)zhishidianView
{
    if (!_zhishidianView) {
        _zhishidianView = [UIView new];
        _zhishidianView.backgroundColor = UIColorFromHex(kColorWhite);
    }
    return _zhishidianView;
}

-(ReportCardTitleView *)zhishidianTitleView
{
    if (!_zhishidianTitleView) {
        _zhishidianTitleView = [ReportCardTitleView new];
        [_zhishidianTitleView configViewWithTitle:@"知识点"];
    }
    return _zhishidianTitleView;
}

-(UILabel *)zhishidianLabel
{
    if (!_zhishidianLabel) {
        _zhishidianLabel = [UILabel new];
        _zhishidianLabel.textColor = UIColorFromHex(0x000000);
        _zhishidianLabel.font = UIFontSize(13);
    }
    return _zhishidianLabel;
}

-(DaTiYongShiView *)yongshiView
{
    if (!_yongshiView) {
        _yongshiView = [DaTiYongShiView new];
    }
    return _yongshiView;
}

- (JunFenQingKuangView *)junfenView
{
    if (!_junfenView) {
        _junfenView = [JunFenQingKuangView new];
    }
    return _junfenView;
}

-(UIView *)jeixiView
{
    if (!_jeixiView) {
        _jeixiView = [UIView new];
        _jeixiView.backgroundColor = UIColorFromHex(kColorWhite);
    }
    return _jeixiView;
}

-(ReportCardTitleView *)jiexiTitleView
{
    if (!_jiexiTitleView) {
        _jiexiTitleView = [ReportCardTitleView new];
        [_jiexiTitleView configViewWithTitle:@"解析"];
    }
    return _jiexiTitleView;
}

-(RCDWebView *)myWebView
{
    if (!_myWebView) {
        _myWebView = [RCDWebView new];
//        _myWebView.delegate = self;
        _myWebView.scrollView.scrollEnabled = NO;
        _myWebView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

    }
    return _myWebView;
}

-(void)dealloc
{
    [self.myWebView.scrollView removeObserver:self forKeyPath:@"contentSize" context:nil];
}

@end
